<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/15
 * Time: 14:15
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Word extends Base {
    //
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['s_word'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.s_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.s_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.s_time'] = ['between time',[$start,$end]];
            }
            $count = Db::table('up_sensitive')->alias('a')
                ->where($map)
                ->count();
            $list = Db::table('up_sensitive')->alias('a')
                ->where($map)
                ->order('s_time desc')
                ->page($page, $limit)
                ->select();//计算总页面
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    //添加
    public function add(){
        if(request()->isPost()){
            $word = input('param.s_word');
            $data['s_word'] = $word;
            $data['s_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_sensitive')->insertGetId($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }


        //删除
        public function del(){
            $id = input('param.id');
            $res = Db::table('up_sensitive')->where('s_id',$id)->delete();
            if($res){
                return json(['code' => 200, 'msg' => '删除成功']);
            }else{
                return json(['code' => 100, 'msg' => '删除失败']);
            }
    }
}
